package edu.gatech.cs2340.thecyclonesquad.database;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import edu.gatech.cs2340.thecyclonesquad.models.InMemoryModel;
import edu.gatech.cs2340.thecyclonesquad.models.Item;
import edu.gatech.cs2340.thecyclonesquad.models.Person;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;

@SuppressLint("SimpleDateFormat")
public class Database {
	
	private MyDBHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;
	
	/**
	 * This method is the constructor
	 * @param c is context passed to constructor
	 */
	public Database(Context c) {
		ourContext = c;
	}
	
	/**
	 * This method opens database.
	 * @return instance of database
	 * @throws SQLException if database access error
	 */
	public Database open() throws SQLException {
		ourHelper = new MyDBHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();//opens database
		return this;
	}
	
	/**
	 * This method closes database.
	 */
	public void close() {
		ourHelper.close();
	}

	/**
	 * Creates a person.
	 * @param username is the new user's name
	 * @param password is the password of user
	 * @param admin is true or false
	 * @return the row of table
	 */
	public long createPerson(String username, String password, String admin) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
		cv.put(MyDBHelper.COLUMN_USERNAME,username);
		cv.put(MyDBHelper.COLUMN_PASSWORD, password);
		cv.put(MyDBHelper.COLUMN_LOGINATTEMPTS, "0");
		cv.put(MyDBHelper.COLUMN_LOCKED, "false");
		cv.put(MyDBHelper.COLUMN_ADMIN, admin);
		return ourDatabase.insert(MyDBHelper.TABLE_NAME, null, cv);
		
	}

	/**
	 * Checks if user in system.
	 * @param username is the string of the username
	 * @return true if in system or false if not
	 */
	public boolean isThere(String username) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID, MyDBHelper.COLUMN_USERNAME, MyDBHelper.COLUMN_PASSWORD, MyDBHelper.COLUMN_LOGINATTEMPTS, MyDBHelper.COLUMN_LOCKED, MyDBHelper.COLUMN_ADMIN};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME, columns, null, null, null, null, null);
		boolean result = false;
		String username2;
		int iUsername = c.getColumnIndex(MyDBHelper.COLUMN_USERNAME);
		
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			username2 = c.getString(iUsername);
			if (username2.equals(username)) {
				result = true;
			}
		}
		c.close();
		return result;
	}
	
	/**
	 * Verify if password is correct or not.
	 * @param username is the user trying to login
	 * @param password is the password string typed in the login screen
	 * @return number signifying the different states possible
	 */
	public int verify(String username, String password) {//not right, doing nothing with loginAttempts
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID, MyDBHelper.COLUMN_USERNAME, MyDBHelper.COLUMN_PASSWORD, MyDBHelper.COLUMN_LOGINATTEMPTS, MyDBHelper.COLUMN_LOCKED, MyDBHelper.COLUMN_ADMIN};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME, columns, null, null, null, null, null);//read from database
		String username2;
		String password2 = "";
		String loginAttempts2 ="";
		int loginAttempts3 = 0;
		String locked2 = "";
		String admin2 = "";
		long l = 0;
		int result = 0;
		ContentValues cvUpdate = new ContentValues();//to update row
		
		//gets the index of each column
		int iRow = c.getColumnIndex(MyDBHelper.COLUMN_ID);
		int iUsername = c.getColumnIndex(MyDBHelper.COLUMN_USERNAME);
		int iPassword = c.getColumnIndex(MyDBHelper.COLUMN_PASSWORD);
		int iLoginAttempts = c.getColumnIndex(MyDBHelper.COLUMN_LOGINATTEMPTS);
		int iLocked = c.getColumnIndex(MyDBHelper.COLUMN_LOCKED);
		int iAdmin = c.getColumnIndex(MyDBHelper.COLUMN_ADMIN);
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			username2 = c.getString(iUsername);
			if (username2.equals(username)) {
				password2 = c.getString(iPassword);
				loginAttempts2 = c.getString(iLoginAttempts);
				loginAttempts3 = Integer.parseInt(loginAttempts2);
				locked2 = c.getString(iLocked);
				admin2 = c.getString(iAdmin);
				l = Long.parseLong(c.getString(iRow));
			}
		}
		if (password.equals(password2) & locked2.equals("false") & admin2.equals("true")) {
			loginAttempts3 = 0;
			result = 1;//correct login and admin
		} else if (password.equals(password2) & locked2.equals("false") & admin2.equals("false")) {
			loginAttempts3 = 0;
			result = 2;//correct login and not admin
		} else if (locked2.equals("true")) {
			result = 3; //user is locked
		} else if (!password.equals(password2) & locked2.equals("false")) {
			loginAttempts3 = loginAttempts3 + 1;
			if (loginAttempts3 == 3) {//locked if 3 wrong attempts
				locked2 = "true";
			}
			result = 4;//incorrect password
		} 
		
		//cvUpdate.put(MyDBHelper.COLUMN_USERNAME, username);
		//cvUpdate.put(MyDBHelper.COLUMN_PASSWORD, password2);
		cvUpdate.put(MyDBHelper.COLUMN_LOGINATTEMPTS, Integer.toString(loginAttempts3));
		cvUpdate.put(MyDBHelper.COLUMN_LOCKED, locked2);
		//cvUpdate.put(MyDBHelper.COLUMN_ADMIN, admin2);
		ourDatabase.update(MyDBHelper.TABLE_NAME, cvUpdate, MyDBHelper.COLUMN_ID + "=" + l, null);
		c.close();
		return result;
		/*else if (loginAttempts2 >= "3") {
			//locked = true;
            return 2;
        } else{
        	if (loginAttempts2 == "2") {
        		locked = true;
        	}
            return 3;
        }*/
		
	}

	/**
	 * Method to get the users that are locked.
	 * @return all locked users
	 */
	public String[] getLockedOutUsers() {
		String[] columns = new String[] {MyDBHelper.COLUMN_ID, MyDBHelper.COLUMN_USERNAME, MyDBHelper.COLUMN_PASSWORD, MyDBHelper.COLUMN_LOGINATTEMPTS, MyDBHelper.COLUMN_LOCKED, MyDBHelper.COLUMN_ADMIN};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME, columns, null , null, null, null, null);
		int iUsername = c.getColumnIndex(MyDBHelper.COLUMN_USERNAME);
		int iLocked = c.getColumnIndex(MyDBHelper.COLUMN_LOCKED);
		int size = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iLocked).equals("true")) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iLocked).equals("true")) {
					retVal[x++] = c.getString(iUsername);
				}
			}
		}		
		c.close();
		return retVal;
		// TODO Auto-generated method stub
	}

	/**
	 * Unlocks the user.
	 * @param dude is the locked user
	 */
	public void unlockUser(String dude) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID, MyDBHelper.COLUMN_USERNAME, MyDBHelper.COLUMN_PASSWORD, MyDBHelper.COLUMN_LOGINATTEMPTS, MyDBHelper.COLUMN_LOCKED, MyDBHelper.COLUMN_ADMIN};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME, columns, null, null, null, null, null);//read from database
		long l = 0;
		String username2 = "";
		ContentValues cvUpdate = new ContentValues();//to update row
		
		//gets the index of each column
		int iRow = c.getColumnIndex(MyDBHelper.COLUMN_ID);
		int iUsername = c.getColumnIndex(MyDBHelper.COLUMN_USERNAME);
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			username2 = c.getString(iUsername);
			if (username2.equals(dude)) {
				l = Long.parseLong(c.getString(iRow));
			}
		}
		cvUpdate.put(MyDBHelper.COLUMN_LOGINATTEMPTS, "0");
		cvUpdate.put(MyDBHelper.COLUMN_LOCKED, "false");
		ourDatabase.update(MyDBHelper.TABLE_NAME, cvUpdate, MyDBHelper.COLUMN_ID + "=" + l, null);
		c.close();
		
	}

	/**
	 * This method deletes user from system
	 * @param user is string of user to be deleted
	 */
	public void deleteUser(String user) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID, MyDBHelper.COLUMN_USERNAME, MyDBHelper.COLUMN_PASSWORD, MyDBHelper.COLUMN_LOGINATTEMPTS, MyDBHelper.COLUMN_LOCKED, MyDBHelper.COLUMN_ADMIN};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME, columns, null, null, null, null, null);//read from database
		long l = 0;
		long l2 = 0;
		String username2 = "";
		//gets the index of each column
		int iRow = c.getColumnIndex(MyDBHelper.COLUMN_ID);
		int iUsername = c.getColumnIndex(MyDBHelper.COLUMN_USERNAME);
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			username2 = c.getString(iUsername);
			if (username2.equals(user)) {
				l = Long.parseLong(c.getString(iRow));
			}
		}
		ourDatabase.delete(MyDBHelper.TABLE_NAME, MyDBHelper.COLUMN_ID + "=" + l, null);
		c.close();
		String[] columns2 = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c2 = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns2, null , null, null, null, null);
		int iRow2 = c2.getColumnIndex(MyDBHelper.COLUMN_ID2);
		int iOwner = c2.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) {
			username2 = c2.getString(iOwner);
			if (username2.equals(user)) {
				l2 = Long.parseLong(c2.getString(iRow2));
				ourDatabase.delete(MyDBHelper.TABLE_NAME2, MyDBHelper.COLUMN_ID2 + "=" + l2, null);//items delete
			}
		}
		c2.close();
	}

	/**
	 * Creates an item.
	 * @param owner is person posting item
	 * @param name is name of item
	 * @param description is description of item
	 * @param location is where item is currently
	 * @param reward is how much person finding item will get paid
	 * @param date is when item posted
	 * @param type is lost or found
	 * @param category is the kind of item
	 * @param status is if claimed or not
	 * @return the id in table
	 */
	public long createItem(String owner, String name, String description,
			String location, String reward, Date date, String type,
			String category, boolean status) {
		// TODO Auto-generated method stub
		String status2;
		if (status == true) {
			status2 = "true";
		} else {
			status2 = "false";
		}
		ContentValues cv = new ContentValues();
		cv.put(MyDBHelper.COLUMN_OWNER,owner);
		cv.put(MyDBHelper.COLUMN_NAME, name);
		cv.put(MyDBHelper.COLUMN_DESCRIPTION, description);
		cv.put(MyDBHelper.COLUMN_LOCATION, location);
		cv.put(MyDBHelper.COLUMN_REWARD, reward);
		cv.put(MyDBHelper.COLUMN_DATE, date.toString());
		cv.put(MyDBHelper.COLUMN_TYPE, type);
		cv.put(MyDBHelper.COLUMN_CATEGORY, category);
		cv.put(MyDBHelper.COLUMN_STATUS, status2);    //need to figure out how claimed
		return ourDatabase.insert(MyDBHelper.TABLE_NAME2, null, cv);
		
	}

	/**
	 * This method gets all the items depending on specifications.
	 * @param type is if lost or found
	 * @return the correct items
	 */
	public String[] getItems(String type) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int size = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iType).equals(type) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iType).equals(type) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					retVal[x++] = c.getString(iName);
				}
			}
		}		
		c.close();
		return retVal;
	}

	/**
	 * Checks if item in system.
	 * @param owner is person who has item
	 * @param name is item's name
	 * @param type is lost or found
	 * @return true if there or false if not
	 */
	public boolean itemThere(String owner, String name, String type) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		boolean result = false;		
		//int iRow = c.getColumnIndex(MyDBHelper.COLUMN_ID);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			if ((c.getString(iOwner).equals(owner)) && (c.getString(iName).equals(name)) && (c.getString(iType).equals(type))) {
				result = true;
			}
		}
		c.close();
		return result;

	}

	/**
	 * Gets the wanted items from system.
	 * @param type is lost or found
	 * @param category is what kind of item
	 * @return the wanted items
	 */
	public String[] getItems(String type, String category) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int iCategory = c.getColumnIndex(MyDBHelper.COLUMN_CATEGORY);
		int size = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iType).equals(type) && c.getString(iCategory).equals(category) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iType).equals(type) && c.getString(iCategory).equals(category) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					retVal[x++] = c.getString(iName);
				}
			}
		}		
		c.close();
		return retVal;
	}

	/**
	 * Gets the wanted items from system.
	 * @param type is lost or found
	 * @param status is if claimed 
	 * @return the wanted items
	 */
	public String[] getItems(String type, boolean status) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int iStatus = c.getColumnIndex(MyDBHelper.COLUMN_STATUS);
		int size = 0;
		String status2;
		if (status == true) {
			status2 = "true";
		} else {
			status2 = "false";
		}
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iType).equals(type) && c.getString(iStatus).equals(status2) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
			
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iType).equals(type) && c.getString(iStatus).equals(status2) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					retVal[x++] = c.getString(iName);
				}
			}
		}		
		c.close();
		return retVal;
	}

	/**
	 * Gets the wanted items from system.
	 * @param type is lost or found
	 * @param date is when posted
	 * @return the wanted items
	 */
	@SuppressWarnings("deprecation")
	public String[] getItems(String type, Date date) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int iDate = c.getColumnIndex(MyDBHelper.COLUMN_DATE);
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int size = 0;
		String date2;
		Date date3;
		if (c != null && date != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				date2 = c.getString(iDate);
				date3 = new Date(date2);
				//try {
					//date3 = sdf.parse(date2);
					//if (c.getString(iType).equals(type) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					if (c.getString(iType).equals(type) && date3.compareTo(date)>0 && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
						size++;
					}
				//} catch (ParseException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				//}

			}
		}			
		String[] retVal = new String[size];
		int x = 0;
			
		if (c != null && date != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				date2 = c.getString(iDate);
				date3 = new Date(date2);
				//try {
					//date3 = sdf.parse(date2);
					//if (c.getString(iType).equals(type) && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
					if (c.getString(iType).equals(type) && date3.compareTo(date)>0 && c.getString(iOwner).equals(InMemoryModel.currentUser)) {
						retVal[x++] = c.getString(iName);
					}
				//} catch (ParseException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				//}
			}
		}		
		c.close();
		return retVal;
	}

	/**
	 * Search for the item by name.
	 * @param name is the item's name
	 * @return the wanted items
	 */
	public String[] searchName(String name) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int iLocation = c.getColumnIndex(MyDBHelper.COLUMN_LOCATION);
		int size = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iName).equals(name)) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iName).equals(name)) {
					retVal[x++] = c.getString(iName) + " " + c.getString(iType) + " by " + c.getString(iOwner) + " in " + c.getString(iLocation) + ".";  
				}
			}
		}		
		c.close();
		return retVal;

	}

	/**
	 * Search for the item by category.
	 * @param category is the kind of item
	 * @return the wanted item
	 */
	public String[] searchCategory(String category) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int iCategory = c.getColumnIndex(MyDBHelper.COLUMN_CATEGORY);
		int iLocation = c.getColumnIndex(MyDBHelper.COLUMN_LOCATION);
		int size = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iCategory).equals(category)) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if (c.getString(iCategory).equals(category)) {
					retVal[x++] = c.getString(iName) + " " + c.getString(iType) + " by " + c.getString(iOwner) + " in " + c.getString(iLocation) + ".";  
				}
			}
		}		
		c.close();
		return retVal;

	}

	/**
	 * Finds the matching found items.
	 * @param name of item to find
	 * @return matching items
	 */
	public String[] matchName(String name) {
		// TODO Auto-generated method stub
		String[] columns = new String[] {MyDBHelper.COLUMN_ID2, MyDBHelper.COLUMN_OWNER, MyDBHelper.COLUMN_NAME, MyDBHelper.COLUMN_DESCRIPTION, MyDBHelper.COLUMN_LOCATION, MyDBHelper.COLUMN_REWARD, MyDBHelper.COLUMN_DATE, MyDBHelper.COLUMN_TYPE, MyDBHelper.COLUMN_CATEGORY, MyDBHelper.COLUMN_STATUS};
		Cursor c = ourDatabase.query(MyDBHelper.TABLE_NAME2, columns, null , null, null, null, null);
		int iOwner = c.getColumnIndex(MyDBHelper.COLUMN_OWNER);
		int iName = c.getColumnIndex(MyDBHelper.COLUMN_NAME);
		int iType = c.getColumnIndex(MyDBHelper.COLUMN_TYPE);
		int iLocation = c.getColumnIndex(MyDBHelper.COLUMN_LOCATION);
		int size = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if ((c.getString(iName).equals(name)) && (c.getString(iType).equals("Found"))) {
					size++;
				}
			}
		}			
		String[] retVal = new String[size];
		int x = 0;
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				if ((c.getString(iName).equals(name)) && (c.getString(iType).equals("Found"))) {
					retVal[x++] = c.getString(iOwner) + " found " + c.getString(iName) + " in " + c.getString(iLocation) + ".";  
				}
			}
		}
		c.close();
		return retVal;
	}
	
}
